package com.yjdb.sanyuantang.modules.Admin.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.yjdb.sanyuantang.aop.OperationLogAnnotation;
import com.yjdb.sanyuantang.common.entity.SysLimit;
import com.yjdb.sanyuantang.common.result.CommonResult;
import com.yjdb.sanyuantang.common.result.ResultCode;
import com.yjdb.sanyuantang.modules.Admin.DTO.limit.LimitDTO;
import com.yjdb.sanyuantang.modules.Admin.DTO.limit.LimitListDTO;
import com.yjdb.sanyuantang.modules.Admin.service.AdminLimitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * packageName com.yjdb.sanyuantang.modules.Admin.controller
 *
 * @author 混沌酱
 * @version 1.0
 * @className AdminLimitController
 * @date 2024/3/15
 * @description TODO
 */
@Tag(name = "后台权限管理")
@RequestMapping("/admin/limit")
@RestController
public class AdminLimitController {

    private static final Logger logger = LoggerFactory.getLogger(AdminRoleController.class);

    @Resource
    private AdminLimitService adminLimitService;

    //添加权限
    @OperationLogAnnotation(operModule = "后台权限管理", operFunction = "添加权限")
    @Operation(summary = "添加权限")
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    public CommonResult<?> addRole(@RequestBody LimitDTO limitDTO){
        try{
            adminLimitService.addRole(limitDTO);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            logger.debug("添加失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }

    // 修改权限
    @OperationLogAnnotation(operModule = "后台权限管理", operFunction = "修改权限")
    @Operation(summary = "修改权限")
    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public CommonResult<?> updateRole(@RequestBody SysLimit sysLimit){
        try{
            adminLimitService.updateRole(sysLimit);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            logger.debug("修改失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }

    // 删除权限
    @OperationLogAnnotation(operModule = "后台权限管理", operFunction = "删除权限")
    @Operation(summary = "删除权限")
    @RequestMapping(path = "/delete", method = RequestMethod.GET)
    public CommonResult<?> delete(
            @Parameter(name = "id",description = "权限id",required = true)  Integer id
    ){
        try{
            adminLimitService.delete(id);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            logger.debug("删除失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }


    // 查询权限
    @Operation(summary = "查询权限")
    @RequestMapping(path = "/list", method = RequestMethod.GET)
    @SaIgnore
    public CommonResult<List<LimitListDTO>> list(){
        try{
            List<LimitListDTO> list = adminLimitService.list();
            return CommonResult.success(list);
        } catch (Exception e) {
            logger.debug("查询失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }




}
